package com.ruoyi.pm.service.impl;

import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.common.utils.pubfun.IdWorker;
import com.ruoyi.framework.util.LoginUserUtil;
import com.ruoyi.pm.domain.ExcPort;
import com.ruoyi.pm.domain.ExcPortHeader;
import com.ruoyi.pm.mapper.ExcPortMapper;
import com.ruoyi.pm.service.IExcPortConfigService;
import com.ruoyi.pm.service.IExcPortHeaderService;
import com.ruoyi.pm.service.IExcPortService;
import com.ruoyi.pm.service.IExcPortUpdateConditionService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * 保司接口配置Service业务层处理
 *
 * @author ruoyi
 * @date 2022-01-20
 */
@Service
public class ExcPortServiceImpl implements IExcPortService {
    @Autowired
    private ExcPortMapper excPortMapper;

    @Autowired
    private IExcPortConfigService excPortConfigService;

    @Autowired
    private IExcPortHeaderService excPortHeaderService;

    @Autowired
    private IExcPortUpdateConditionService excPortUpdateConditionService;

    /**
     * 查询保司接口配置
     *
     * @param id 保司接口配置主键
     * @return 保司接口配置
     */
    @Override
    public ExcPort selectExcPortById(Long id) {
        return excPortMapper.selectExcPortById(id);
    }

    /**
     * 查询保司接口配置列表
     *
     * @param excPort 保司接口配置
     * @return 保司接口配置
     */
    @Override
    public List<ExcPort> selectExcPortList(ExcPort excPort) {
        return excPortMapper.selectExcPortList(excPort);
    }

    /**
     * 新增保司接口配置
     *
     * @param excPort 保司接口配置
     * @return 结果
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public int insertExcPort(ExcPort excPort) {
        excPort.setId(IdWorker.getId());
        excPort.setCreateBy(LoginUserUtil.getSysUser().getUserName());
        excPort.setCreateTime(DateUtils.getNowDate());
        excPort.setUpdateBy(LoginUserUtil.getSysUser().getUserName());
        excPort.setUpdateTime(DateUtils.getNowDate());
        String dataCoding = excPort.getDataCoding();
        if(StringUtils.isNotEmpty(dataCoding)){
            ExcPortHeader excPortHeader = new ExcPortHeader();
            excPortHeader.setPortId(excPort.getId());
            excPortHeader.setField("Content-Type");
            excPortHeader.setFieldValue(dataCoding);
            excPortHeaderService.insertExcPortHeader(excPortHeader);
        }
        return excPortMapper.insertExcPort(excPort);
    }

    /**
     * 修改保司接口配置
     *
     * @param excPort 保司接口配置
     * @return 结果
     */
    @Override
    public int updateExcPort(ExcPort excPort) {
        excPort.setUpdateBy(LoginUserUtil.getSysUser().getUserName());
        excPort.setUpdateTime(DateUtils.getNowDate());
        String dataCoding = excPort.getDataCoding();
        if(StringUtils.isNotEmpty(dataCoding)){
            ExcPortHeader excPortHeader = new ExcPortHeader();
            excPortHeader.setPortId(excPort.getId());
            excPortHeader.setField("Content-Type");
            excPortHeader.setFieldValue(dataCoding);
            ExcPortHeader portHeader = excPortHeaderService.selectByPortIdAndField(excPort.getId(), "Content-Type");
            if(StringUtils.isNotNull(portHeader)){
                excPortHeader.setId(portHeader.getId());
                excPortHeaderService.updateExcPortHeader(excPortHeader);
            }else {
                excPortHeaderService.insertExcPortHeader(excPortHeader);
            }
        }

        return excPortMapper.updateExcPort(excPort);
    }

    /**
     * 批量删除保司接口配置
     *
     * @param ids 需要删除的保司接口配置主键
     * @return 结果
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public int deleteExcPortByIds(Long[] ids) {
        for (Long id: ids) {
            //先删除config
            excPortConfigService.deleteExcPortConfigByPortId(id);
            //删除header
            excPortHeaderService.deleteExcPortHeaderByPortId(id);
            //删除规则配置
            excPortUpdateConditionService.deleteByPortId(id);
        }
        return excPortMapper.deleteExcPortByIds(ids);
    }

    /**
     * 删除保司接口配置信息
     *
     * @param id 保司接口配置主键
     * @return 结果
     */
    @Override
    public int deleteExcPortById(Long id) {
        //先删除config
        excPortConfigService.deleteExcPortConfigByPortId(id);
        //删除header
        excPortHeaderService.deleteExcPortHeaderByPortId(id);
        //删除规则配置
        excPortUpdateConditionService.deleteByPortId(id);
        return excPortMapper.deleteExcPortById(id);
    }
}
